package com.yanqu.road.server.gameplayer.module.activity.horserun.event;

import com.yanqu.road.entity.activity.horserun.HorseRunUserGamingData;
import com.yanqu.road.entity.activity.horserun.config.HorseRunEventConfig;
import com.yanqu.road.logic.activity.horserun.HorseRunConfig;
import com.yanqu.road.pb.activity.HorseRunProto;
import com.yanqu.road.server.manger.activity.horserun.HorseRunMgr;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.util.ArrayList;
import java.util.List;

/**
 * train("二选一事件,经验和其中一个随机属性",4),
 */
public class HorseRunTrainEvent extends HorseRunBaseEvent{
    @Override
    public HorseRunProto.EventResult.Builder meet(HorseRunUserGamingData userGamingData, HorseRunConfig config,HorseRunEventConfig eventConfigById) {
        HorseRunProto.EventResult.Builder builder = HorseRunProto.EventResult.newBuilder();
        int eventId = eventConfigById.getId();

        builder.setError(0);

//        int attType = HorseRunMgr.randomAttrType();

        int attType = eventConfigById.getParamList().get(0);
        userGamingData.setChooseOneEventAttrType(attType);

        //加到日志里面
        ArrayList<Integer> param = new ArrayList<>();
        param.add(attType);
        addUserLog(userGamingData,config,1,eventId, param,builder);

        eventGoing(userGamingData);

        return builder;
    }

    @Override
    public HorseRunProto.EventResult.Builder deal(HorseRunUserGamingData userGamingData, HorseRunDealEventArg arg, HorseRunConfig config,HorseRunEventConfig eventConfigById) {
        HorseRunProto.EventResult.Builder builder = HorseRunProto.EventResult.newBuilder();
        int eventId = eventConfigById.getId();
        if (arg.getChooseOneArg() != 1 && arg.getChooseOneArg() != 2){
            builder.setError(GameErrorCode.E_HORSE_RUN_ARG_ERROR);
            return builder;
        }

        builder.setError(0);

        int addType = 0;
        int addNum = 0;

        if (arg.getChooseOneArg() == 1){
            addType = 0;
            addNum = eventConfigById.getParamList().get(3) * userGamingData.getOpenGameMultiple();
            userGamingData.addExpSum(addNum);
            builder.setExpAdd(addNum);

        }else {
            addType = userGamingData.getChooseOneEventAttrType();
            addNum = eventConfigById.getParamList().get(1);

            userGamingData.addBaseAttr(addType,addNum);

            HorseRunProto.AttrRiseResult.Builder attrBuild = HorseRunProto.AttrRiseResult.newBuilder();
            attrBuild.setType(addType);
            attrBuild.setValue(addNum);

            builder.addAttrAdd(attrBuild);
        }
        //无论如何清掉
        userGamingData.setChooseOneEventAttrType(0);

        List<Integer> logParam = new ArrayList<>();
        logParam.add(addType);        //经验 第一个参数0
        logParam.add(addNum);        //加了多少 第二个参数

        addUserLog(userGamingData,config,2,eventId,logParam,builder);
        eventDone(userGamingData);

        return builder;
    }
}
